package com.zzc.emotisenseclassroom.domain.user.utils;

import com.zzc.ZzcException.IllegalInstantiationException;
import com.zzc.emotisenseclassroom.common.domainexception.AccountNumberNotExistsException;
import com.zzc.emotisenseclassroom.common.domainexception.UserIdAlreadyExists;
import com.zzc.emotisenseclassroom.dbservice.user.UserDbService;
import com.zzc.emotisenseclassroom.dto.user.UserDTO;
import com.zzc.springContextUtils.SpringBeanUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * Copyright Zzc
 * FileName: UserUtils
 * Author:   Zzc
 * Date:     2024/4/2721:10
 * Description:用户工具类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 * Zzc                   1.0.0
 */
public class UserUtils {
    /**
     * 工具类禁止实例化
     */
    private UserUtils(){
        throw new IllegalInstantiationException();
    }

    public static void checkUserInfo(UserDTO userDTO){
        UserDbService userDbService = SpringBeanUtil.getBean(UserDbService.class);

        // 校验账号
        Long accountNumber = userDbService.checkAccountNumberIsExists(Long.valueOf(userDTO.getAccountNumber()));

        if (accountNumber == null){
            throw new AccountNumberNotExistsException();
        }

        // 校验工号，同一个学校同一个班级，工号不能重复
        if (StringUtils.isNotBlank(userDTO.getSchoolName()) && StringUtils.isNotBlank(userDTO.getClassName())
                && userDTO.getUserId() != null){
            UserDTO userInfo= userDbService.getUserInfoByUserId(userDTO.getSchoolId(), userDTO.getClassId(), userDTO.getUserId());
            if (userInfo != null){
                if (!Objects.equals(userInfo.getAccountNumber(), userDTO.getAccountNumber())){
                    throw new UserIdAlreadyExists(userDTO.getSchoolName());
                }
            }
        }
    }


}
